<?php
namespace Wap\Controller;
use Think\Controller;
use Think\Upload;

use Wap\Service\WeixinService;
use Wap\Service\RuleService;
use Wap\Service\CookieService;
use Wap\Service\SessionService;
use Wap\Service\UploadService;

class ShowController extends Controller {

	public function index(){
		$code = I( "get.code", "", "" );
		if( !empty( $code ) ){
			WeixinService::flush_weixin_openid( $code );
		}

		$this->display( "photophone" );
	}

	public function weixin_enter()
	{
		$url	=	array( "origin" => "", "redirect" => "" );
		//$url['origin']	=	"http://183.156.210.91:808/show/index";
		//$url['origin']	=	"http://183.156.245.88:808/show/index";
		$url['origin']	=	"http://esm.zbxinmeiti.com/show/index";
		$url	=	WeixinService::fetch_redirect_uri( $url );

		redirect( $url['redirect'] );
		/*$var = 0;
		$test = curl_init();
		curl_setopt($test, CURLOPT_URL, "http://www.jb51.net");
		curl_setopt($test, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($test, CURLOPT_HEADER, 0);
		//$output = curl_exec($test);
		$output = curl_exec($test);
		curl_close($test);
		$this->ajaxReturn( $output );*/
	}

	public function introduction()
	{
		$this->display( "active" );
	}

	/**
	 *函数名: vote
	 *函数功能描述: 依据点赞规则、对单个作品进行点赞
	 *@param 
	 *@return  
	 * */
	public function vote() 
	{
		$config = array(
			'time'	=>	array( "begin_time" => strtotime("now"), "end_time" => strtotime( "+1 week" ) ),
			'sum_num'	=>	50,
			'per_num'	=>	1	
		);
//out out
		$out	=	WeixinService::good_weixin_openid();//opendid
		if( $out['error'] ){
			return $this->ajaxReturn( $out );
		}
		$openid = $out['openid'];
//
		$datas	=	array(//点赞记录信息
			//"time"	=>	strtotime( "+2 week" ),
			"openid"	=>	$openid,//点赞openid
			"time"	=>	time(),//点赞时间
			"updated_time"	=>	time(),
			"ip"	=>	get_client_ip(),
			"pid"	=>	$_POST[ 'pid' ]//点赞的作品
		);

		$result	=	RuleService::is_good( $openid, $datas, $config );
		if( $result['error'] == 0 ){
			$vote	=	D( "vote" );
			$result['updated'] = $vote->insert( $datas );
		}

		$this->ajaxReturn( $result );
	}

	public function insert(){//上传作品信息 

		$arr = CookieService::fetch_arr_by_prefix( "upload" );//作品信息

		$arr['uid']		=	SessionService::id_teacher();
		$arr['add_time']=	time();	
		$arr['status']	=	0;

		$show	=	D( "show" );
		$saved	=	$show->insert( $arr );
		if( $saved['success'] ){
			$prefix = "upload_";
			CookieService::expire_by_prefix( $prefix );
		}

		return $this->ajaxReturn( $saved );
	}

	public function fetch_all_debug(){//TODO: thinkphp 分页技术

		//刷新当前用户openid
		//WeixinService::flush_weixin_openid( $_GET['code'] );
		//flush weixin openid
		/*if( isset( $_GET[ "code" ] ) || !empty( $_GET['code'] ) ){
			WeixinService::flush_weixin_openid( $_GET['code'] );
		}*/

		//$uid	=	I( "session.uid", -1 ); //-1:未知
		$page	=	I( "get.page", 1, "" ); //1:默认从第一页开始读取
		$limit	=	I( "get.limit", 10, "" ); //默认每页读取10个
		$status	=	I( "get.status", 1, "" ); //默认读取审核通过的作品

		$show	=	D( "show" );
		$count	=	$show->count() / $limit;//TODO: should use cache
		$datas	=	$show->order( "votes desc" )->page( $page, $limit )->select();//TODO: should not be here

		$result = array(
			"count"	=>	$count,//总共的页数
			"curren"=>	$page,//当前页
			"next"	=>	$page+1,//TODO:当前页
			"pre"	=>	$page-1,//TODO:前一页
			"size"	=>	count( $datas ), //当前页数据个数
			"datas"	=>	$datas//查询到的数据
		);

		$this->ajaxReturn( $result );
	}

	public function detail(){ //查看一幅具体作品

		$id	=	I( "get.id", "", "" );

		$show	=	D( "show" );
		$datas	=	$show->fetch_all_by_id( $id );
		//$rank	=	$show->fetch_rank_by_votes( $datas['votes'] );

		$percent = $datas['votes'] / $datas['votes'] + $datas['votes_gap'];
		if( $percent == 1 ){
			$percent = "100%";
		}else{
			$percent = $percent."%";
		}
		//$this->assign( "rank", $rank );
		$this->assign( "result", $datas );
		$this->assign( "percent",$percent );
		//$this->assign( "openid", WeixinService::good_weixin_openid() );
		$this->display( "photodetail" );
	}
}
